<template>
  <div id="search">
    <input v-model="searchVal" @blur="outAhntherSearch" @keyup.enter="doSearch" @focus="showSearchAnother" />
    <span class="searchBtn" @click="doSearch">搜索</span>
    <div class="searchanother" v-show="isSearch !== 0">
      <div class="isHistory" v-show="isSearch === 1">
        <div class="searchtit">
          <span>搜索记录</span>
          <i class="fr el-icon-delete"></i>
        </div>
        <div @click="searchVal = '职业健康证件通办'" class="searchHistory">职业健康证件通办</div>
        <div @click="searchVal = '成都市适龄幼儿入园'" class="searchHistory">成都市适龄幼儿入园</div>
        <div @click="searchVal = '国际贸易单统一办理'" class="searchHistory">国际贸易单统一办理</div>
      </div>
      <div class="isMind" v-show="isSearch === 2">
        <div @click="searchVal = '职业健康证件通办'" class="mindlist">职业健康证件通办</div>
        <div @click="searchVal = '职业健康证件通办'" class="mindlist">职业健康证件通办</div>
        <div @click="searchVal = '职业健康证件通办'" class="mindlist">职业健康证件通办</div>
        <div @click="searchVal = '职业健康证件通办'" class="mindlist">职业健康证件通办</div>
        <div @click="searchVal = '职业健康证件通办'" class="mindlist">职业健康证件通办</div>
      </div>
    </div>
  </div>
</template>

<script>
  import { getAssociated } from '@/api/reception/receptionApi';
  export default {
    name: 'Search',
    props: {
      comeVal: String
    },
    data() {
      return {
        searchVal: this.comeVal === undefined ? '' : this.comeVal,
        isSearch: 0 //0：收起面板，1：搜索历史 2：联想搜索
      };
    },
    watch: {
      searchVal(val) {
        this.mindSearch(val);
      }
    },
    methods: {
      //根据搜索内容显示记录或联想
      showSearchAnother() {
        console.log(this.searchVal);
        if (this.searchVal === '') this.isSearch = 1;
        else {
          this.isSearch = 2;
        }
      },
      //失去焦点收起
      outAhntherSearch() {
        setTimeout(() => {
          this.isSearch = 0;
        }, 200);
      },
      //发起联想搜索，三个字以下都为搜索历史
      async mindSearch(val) {
        if (val.length < 3) {
          this.isSearch = 1;
          return;
        } else this.isSearch = 2;
        const data = await getAssociated({ areaCodes: '', keyWord: encodeURI(val) });
        if (data.data.success) {
          return;
        }
      },
      //发起搜索
      doSearch() {
        if (this.$route.path !== '/search')
          this.$router.push({ name: 'search', params: { searchVal: this.searchVal } });
        else this.$emit('transfer', this.searchVal);
      }
    }
  };
</script>

<style lang="scss" scoped>
  #search {
    width: 100%;
    height: 100%;
    padding: 10px 5px;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    background-color: rgba(0, 0, 0, 0.2);
    position: relative;
    border-radius: 10px;
    // z-index: 9999;
    input {
      width: 470px;
      height: 50px;
      padding-left: 30px;
      color: #ffffff;
      font-size: 18px;
      background: none;
    }
    .searchBtn {
      display: inline-block;
      width: 93px;
      height: 50px;
      background: #ffffff;
      opacity: 1;
      border-radius: 10px;
      float: right;
      line-height: 50px;
      text-align: center;
      color: $blue;
      font-size: 18px;
      font-weight: bold;
      cursor: pointer;
    }
    .searchanother {
      position: absolute;
      width: 600px;
      min-height: 100px;
      background-color: #ffffff;
      z-index: 9999;
      top: 75px;
      border-radius: 10px;
      box-shadow: 1px 1px 18px #888888;
      .searchtit {
        padding: 15px;
        span {
          color: #79838f;
          font-weight: bold;
          font-size: 16px;
        }
        i:hover {
          color: $blue;
        }
        i {
          font-size: 16px;
          cursor: pointer;
        }
      }
      .searchHistory:hover {
        color: $blue;
      }
      .searchHistory {
        cursor: pointer;
        text-decoration: underline;
        padding-bottom: 10px;
        padding-left: 15px;
        color: #79838f;
        font-size: 14px;
      }
      .isMind {
        padding-bottom: 20px;
        .mindlist:hover {
          color: $blue;
        }
        .mindlist {
          color: #79838f;
          padding-left: 15px;
          padding-top: 10px;
          cursor: pointer;
        }
      }
    }
  }
</style>
